unit RilLmClasses;

interface

uses Ril, Classes, TypInfo;

type
  TRilComponent = class(TInterfacedObject, IComponent)
  protected
    FComponent: TComponent;
    function GetComponentHandle: TRilHandle;
    function GetWinControlHandle: THandle;
    function GetParent: IComponent;

    function GetPropValueByName(const Name: string; var Value): Boolean;
    
    function SetPropByName(const Name: string; const Value): Boolean;
  public
    constructor Create(AComponent: TComponent); virtual;
  end;

implementation

uses
  SysUtils, Controls;

constructor TRilComponent.Create(AComponent: TComponent);
begin
  FComponent := AComponent;
end;

{ TRilComponent }

function TRilComponent.GetComponentHandle: TRilHandle;
begin
  Result := TRilHandle(FComponent);
end;

function TRilComponent.GetParent: IComponent;
begin
  Result := TRilComponent.Create(FComponent.GetParentComponent);
end;

function TRilComponent.GetPropValueByName(const Name: string;
  var Value): Boolean;
var
  PropInfo: PPropInfo;
  V: Variant;
begin
  Result := False;
  PropInfo := GetPropInfo(FComponent, Name);
  if PropInfo = nil then
    Exit;

  V := GetPropValue(FComponent, PropInfo, True);

  // return the right type
  case PropInfo^.PropType^^.Kind of
    tkInteger, tkChar, tkWChar, tkClass:
      Integer(Value) := V;
    tkEnumeration, tkSet, tkMethod, tkString, tkLString:
      String(Value) := V;
    tkFloat:
      Extended(Value) := V;
    tkWString:
      WideString(Value) := V;
    tkVariant, tkDynArray:
      Variant(Value) := V;
    tkInt64:
      Int64(Value) := V;
  else
    Exit;
  end;
  Result := True;
end;

function TRilComponent.GetWinControlHandle: THandle;
begin
  Result := 0;
  if FComponent is TWinControl then
    Result := THandle(TWinControl(FComponent).Handle);
end;

function TRilComponent.SetPropByName(const Name: string; const Value): Boolean;
begin
  Result := False;
  try
    if TypInfo.IsPublishedProp(FComponent, Name) then
    begin
      SetPropValue(FComponent, Name, Variant(Value));
      Result := True;
    end;
  except

  end;
end;

end.
